﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.ModularFixture.CApMFFixtureBaseTypeData
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using EMPAPPSERVICESLib;
using EMPTYPELIBRARYLib;
using System;
using System.Collections;
using Tecnomatix.Engineering;
using Tecnomatix.Planning;


namespace DnProcessSimulateCommands.ModularFixture;

internal class CApMFFixtureBaseTypeData : CApMFConfigurationData
{
  private ArrayList m_placementPoints;

  internal CApMFFixtureBaseTypeData(string name, string bitmap)
    : base(name, bitmap)
  {
    this.m_placementPoints = new ArrayList();
  }

  internal CApMFFixtureBaseTypeData(string name)
    : base(name)
  {
    this.m_placementPoints = new ArrayList();
  }

  internal override void SaveAttributesToEms(int internalId)
  {
    if (!CApMFAdminUtils.ConfirmCheckOut(internalId))
      return;
    this.SaveCommonAttributesToEms(internalId, "FixtureBase");
    (TxApplication.ActiveDocument.PlatformGlobalServicesProvider as TxEmsGlobalServicesProvider).GetObjectByInternalId(internalId);
    EmpContext empContext;
    empContext.sessionId = TxApplication.ActiveDocument.eMSSessionId;
    EmpFieldValue[] empFieldValueArray = new EmpFieldValue[2];
    empFieldValueArray[0].fieldName = "PlacementPoints";
    empFieldValueArray[1].fieldName = "IntermediateUnits";
    string[] placementPointsArray = this.CreatePlacementPointsArray();
    empFieldValueArray[0].FieldValue = (object) placementPointsArray;
    string[] intermediateUnitsArray = this.CreateIntermediateUnitsArray();
    empFieldValueArray[1].FieldValue = (object) intermediateUnitsArray;
    EmpNodeFieldsValues[] nodeFieldsValuesArray = new EmpNodeFieldsValues[1]
    {
      new EmpNodeFieldsValues()
      {
        Node = {
          objectId = internalId
        },
        FieldsValues = empFieldValueArray
      }
    };
    ((IEmpNodesServices) new EmpNodesServicesClass()).SetFields(ref empContext, ref nodeFieldsValuesArray);
  }

  internal override void LoadAttributesFromEms(int internalId)
  {
    this.LoadCommonAttributesFromEms(internalId);
    EmpContext empContext;
    empContext.sessionId = TxApplication.ActiveDocument.eMSSessionId;
    IEmpNodesServices iempNodesServices = (IEmpNodesServices) new EmpNodesServicesClass();
    string[] strArray = new string[2]
    {
      "PlacementPoints",
      "IntermediateUnits"
    };
    EmpObjectKey[] empObjectKeyArray = new EmpObjectKey[1];
    empObjectKeyArray[0].objectId = internalId;
    EmpFieldInfo[] fields = iempNodesServices.GetFields(ref empContext, ref empObjectKeyArray, ref strArray);
    string[] placementPointsArray = (string[]) fields[0].Value;
    if (placementPointsArray != null)
      this.ParsePlacementPointsArray(placementPointsArray);
    string[] intermediateUnitsArray = (string[]) fields[1].Value;
    if (intermediateUnitsArray == null)
      return;
    this.ParseIntermediateUnitsArray(intermediateUnitsArray);
  }

  private void ParseIntermediateUnitsArray(string[] intermediateUnitsArray)
  {
    for (int index = 0; index < intermediateUnitsArray.Length; index += 2)
    {
      int pointNameIndex = this.FindPointNameIndex(intermediateUnitsArray[index]);
      if (pointNameIndex > -1)
      {
        TxEmsServicesProvider servicesProvider = new TxEmsGlobalServicesProvider().GetObjectByProcessModelId(new TxProcessModelId(intermediateUnitsArray[index + 1])).PlatformServicesProvider as TxEmsServicesProvider;
        ((CApMFPlacementPoint) this.m_placementPoints[pointNameIndex]).IntermediateUnitId = servicesProvider.InternalId;
      }
    }
  }

  private int FindPointNameIndex(string name)
  {
    int pointNameIndex = -1;
    for (int index = 0; index < this.m_placementPoints.Count; ++index)
    {
      if (((CApMFPlacementPoint) this.m_placementPoints[index]).Name.Equals(name))
      {
        pointNameIndex = index;
        break;
      }
    }
    return pointNameIndex;
  }

  private void ParsePlacementPointsArray(string[] placementPointsArray)
  {
    int index1 = 0;
    while (index1 < placementPointsArray.Length)
    {
      string placementPoints1 = placementPointsArray[index1];
      ++index1;
      ArrayList arrayList = new ArrayList();
      for (int index2 = 0; index2 < 6; ++index2)
      {
        string placementPoints2 = placementPointsArray[index1];
        CApMFPlacementPointGridRow placementPointGridRow = new CApMFPlacementPointGridRow();
        placementPointGridRow.Type = CApMFPlacementPointGridRow.GetTypeFromString(placementPoints2);
        switch (placementPointGridRow.Type)
        {
          case PointType.Exact:
            placementPointGridRow.Value = Convert.ToDouble(placementPointsArray[index1 + 1]);
            index1 += 2;
            break;
          case PointType.Step:
            placementPointGridRow.Min = Convert.ToDouble(placementPointsArray[index1 + 1]);
            placementPointGridRow.Max = Convert.ToDouble(placementPointsArray[index1 + 2]);
            placementPointGridRow.Step = Convert.ToDouble(placementPointsArray[index1 + 3]);
            index1 += 4;
            break;
          case PointType.Free:
            placementPointGridRow.Min = Convert.ToDouble(placementPointsArray[index1 + 1]);
            placementPointGridRow.Max = Convert.ToDouble(placementPointsArray[index1 + 2]);
            index1 += 3;
            break;
        }
        arrayList.Add((object) placementPointGridRow);
      }
      this.m_placementPoints.Add((object) new CApMFPlacementPoint(placementPoints1, (CApMFPlacementPointGridRow) arrayList[0], (CApMFPlacementPointGridRow) arrayList[1], (CApMFPlacementPointGridRow) arrayList[2], (CApMFPlacementPointGridRow) arrayList[3], (CApMFPlacementPointGridRow) arrayList[4], (CApMFPlacementPointGridRow) arrayList[5]));
    }
  }

  private string[] CreateIntermediateUnitsArray()
  {
    ArrayList arrayList = new ArrayList();
    for (int index = 0; index < this.m_placementPoints.Count; ++index)
    {
      CApMFPlacementPoint placementPoint = (CApMFPlacementPoint) this.m_placementPoints[index];
      if (placementPoint.IntermediateUnitId > 0)
      {
        arrayList.Add((object) placementPoint.Name);
        ITxPlanningObject objectByInternalId = new TxEmsGlobalServicesProvider().GetObjectByInternalId(placementPoint.IntermediateUnitId);
        arrayList.Add((object) objectByInternalId.ProcessModelId.ExternalId);
      }
    }
    return arrayList.ToArray(typeof (string)) as string[];
  }

  private string[] CreatePlacementPointsArray()
  {
    ArrayList arrayList = new ArrayList();
    for (int index = 0; index < this.m_placementPoints.Count; ++index)
    {
      CApMFPlacementPoint placementPoint = (CApMFPlacementPoint) this.m_placementPoints[index];
      arrayList.AddRange((ICollection) placementPoint.ToArray());
    }
    return arrayList.ToArray(typeof (string)) as string[];
  }

  internal ArrayList PlacementPoints
  {
    get => this.m_placementPoints;
    set => this.m_placementPoints = value;
  }
}
